﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using BrewTarget.DB;
using BrewTarget.Utils;

namespace BrewTarget.Models
{
    public class Water : BeerBase, IComparable<Water>
    {
        public string Name { get; set; }
        public double Amount_In_Liters { get; set; }
        public double Calcium_PPM { get; set; }
        public double BiCarbonate_PPM { get; set; }
        public double Sulfate_PPM { get; set; }
        public double Chloride_PPM { get; set; }
        public double Sodium_PPM { get; set; }
        public double Magnesium_PPM { get; set; }
        public double PH { get; set; }
        public string Notes { get; set; }


        public Water() : base()
        { }

        public Water(IDataReader inDR) : this()
        {
            this.Load(inDR);
        }

        public void Load(IDataReader inDR)
        {
            
            IDKey = As.Integer(inDR["water_pk"]);
            Name = As.String(inDR["name"]);
            Amount_In_Liters = As.Double(inDR["amount"]);
            Calcium_PPM = As.Double(inDR["calcium"]);
            BiCarbonate_PPM = As.Double(inDR["bicarbonate"]);
            Sulfate_PPM = As.Double(inDR["sulfate"]);
            Chloride_PPM = As.Double(inDR["chloride"]);
            Sodium_PPM = As.Double(inDR["sodium"]);
            Magnesium_PPM = As.Double(inDR["magnesium"]);
            PH = As.Double(inDR["PH"]);
            Notes = As.String(inDR["notes"]);
            IsDeleted = As.Boolean(inDR["deleted"]);
            Display = As.Boolean(inDR["display"]);


        }

        public override bool Equals(object inOtherWater)
        {
            if (inOtherWater is Water)
                return Name == ((Water)inOtherWater).Name;
            else
                return base.Equals(inOtherWater);
        }


        public int CompareTo(Water inOtherWater)
        {
            if (inOtherWater == null)
                return 0;
            else
                return inOtherWater.Name.CompareTo(Name);
        }


        private void setDefaults()
        {
            Name = "";
            Amount_In_Liters = 0.0;
            Calcium_PPM = 0.0;
            BiCarbonate_PPM = 0.0;
            Sulfate_PPM = 0.0;
            Chloride_PPM = 0.0;
            Sodium_PPM = 0.0;
            Magnesium_PPM = 0.0;
            PH = 7.0;
            Notes = "";
        }

        public static IEnumerable<Water> GetWaterList(DbHolder inDB)
        {
            List<Water> aList = new List<Water>();

            using (IDataReader aDR = inDB.OpenDataReader("select * from water order by name"))
            {
                while (aDR.Read())
                {
                    Water aWater = new Water(aDR);
                    aList.Add(aWater);
                }

            }

            return aList;
        }

        public static Water GetWater(DbHolder inDB, int inPK)
        {
            Water aWater = new Water();

            using (IDataReader iDR = inDB.OpenDataReader("select * from water where water_pk = :theVal", inPK))
            {
                if (iDR.Read())
                    aWater.Load(iDR);
            }

            return aWater;
        }

        public bool Post(DbHolder inDB)
        {
            string theSQL = "insert into water (name, amount, calcium, bicarbonate, sulfate, chloride, sodium, magnesium, ph, notes, deleted, display) " +
                            "values " +
                            "(:name, :amount, :calcium, :bicarbonate, :sulfate, :chloride, :sodium, :magnesium, :ph, :notes, :deleted, :display)";

            inDB.ExecuteSQL(theSQL, Name, Amount_In_Liters, Calcium_PPM, BiCarbonate_PPM, Sulfate_PPM, Chloride_PPM, Sodium_PPM, Magnesium_PPM, PH, Notes, IsDeleted, Display);
            return true;
        }
    }
}